using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using Spacejock;
using System;
using System.Collections;
using System.IO;
using System.Windows.Forms;
using yW5Lib;

namespace yWriter6
{
	[StandardModule]
	internal sealed class modExportTimeline
	{
		public static void ExportTimeline(clsProject cProject)
		{
			//Discarded unreachable code: IL_0484, IL_049b, IL_04a6, IL_04b6, IL_05d0, IL_05d2, IL_05e6, IL_0608
			int num = default(int);
			int num7 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				Collection collection = new Collection();
				num2 = 3;
				Interaction.MsgBox("Select the character(s) you want to include in the timeline (Ctrl+click for multiple selection)");
				num2 = 4;
				frmList frmList = new frmList(ref cProject, eRecord.eCharacter);
				num2 = 5;
				if (frmList.ShowDialog(AllowSwitching: false) == DialogResult.OK)
				{
					num2 = 6;
					int count = frmList.ListItems.SelectedItems.Count;
					num2 = 7;
					if (count == 0)
					{
						num2 = 8;
						clsShared.cLang.TransMsgBox("No characters selected.");
					}
					else
					{
						num2 = 10;
						string filename = cProject.ProjectPath() + "Timeline.html";
						num2 = 11;
						StreamWriter streamWriter = clsShared.cFile.OpenOutputStream(filename);
						num2 = 12;
						if (streamWriter == null)
						{
							num2 = 13;
							clsShared.cLang.TransMsgBox("Could not create output file. Already open in another program?");
						}
						else
						{
							num2 = 15;
							clsShared.cSubs.BusyMouse();
							num2 = 16;
							streamWriter.Write("<HTML><HEAD>");
							num2 = 17;
							streamWriter.Write("<Title>" + cProject.Title + " by " + cProject.AuthorName + " (Exported " + Conversions.ToString(DateAndTime.Now) + ")</title>");
							num2 = 18;
							streamWriter.WriteLine("</HEAD><BODY>");
							num2 = 19;
							streamWriter.WriteLine("<font size=+2><br /><center>");
							num2 = 20;
							streamWriter.WriteLine(cProject.Title + " by " + cProject.AuthorName);
							num2 = 21;
							streamWriter.WriteLine("</font></center>");
							num2 = 22;
							streamWriter.WriteLine("<br /><br />");
							num2 = 23;
							streamWriter.WriteLine("<table width=100% border=1 cellspacing=2 cellpadding=5>");
							num2 = 24;
							streamWriter.WriteLine("<TR>");
							num2 = 25;
							IEnumerator enumerator = frmList.ListItems.SelectedItems.GetEnumerator();
							while (enumerator.MoveNext())
							{
								ListViewItem listViewItem = (ListViewItem)enumerator.Current;
								num2 = 26;
								streamWriter.WriteLine("<TD align=center><B>" + listViewItem.Text + "</b></TD>");
								num2 = 27;
								collection.Add(listViewItem.Text);
								num2 = 28;
							}
							num2 = 29;
							if (enumerator is IDisposable)
							{
								(enumerator as IDisposable).Dispose();
							}
							num2 = 30;
							streamWriter.WriteLine("</TR>");
							num2 = 31;
							IEnumerator enumerator2 = cProject.AllChaptersUsedNoInfoType().GetEnumerator();
							while (enumerator2.MoveNext())
							{
								clsChapter clsChapter = (clsChapter)enumerator2.Current;
								num2 = 32;
								streamWriter.WriteLine("<TR><TD align=center colspan = " + Conversions.ToString(collection.Count) + "> " + clsChapter.Title + "</TD></TR>");
								num2 = 33;
								int sceneCount = clsChapter.SceneCount;
								for (int i = 1; i <= sceneCount; i++)
								{
									num2 = 34;
									clsScene clsScene = cProject.get_SceneByChID(clsChapter.ID, i);
									num2 = 35;
									clsCharacter clsCharacter = cProject.get_CharByID(clsScene.ViewPointID);
									num2 = 36;
									int num3 = 0;
									num2 = 37;
									int count2 = collection.Count;
									for (int j = 1; j <= count2; j++)
									{
										num2 = 38;
										string right = Conversions.ToString(collection[j]);
										num2 = 39;
										if (Operators.CompareString(clsCharacter.Title, right, TextCompare: false) == 0)
										{
											num2 = 40;
											num3 = j;
											break;
										}
										num2 = 42;
									}
									num2 = 43;
									if (num3 > 0)
									{
										num2 = 44;
										streamWriter.WriteLine("<TR>");
										num2 = 45;
										int num4 = num3 - 1;
										for (int k = 1; k <= num4; k++)
										{
											num2 = 46;
											streamWriter.Write("<td>&nbsp;</td>");
											num2 = 47;
										}
										num2 = 48;
										if (Operators.CompareString(cProject.get_SceneByChID(clsChapter.ID, i).DescText, "", TextCompare: false) == 0)
										{
											num2 = 49;
											streamWriter.Write("<td>" + clsScene.Title + "</td>");
										}
										else
										{
											num2 = 51;
											streamWriter.Write("<td>" + clsScene.DescText + "</td>");
										}
										num2 = 52;
										int num5 = num3 + 1;
										int num6 = count;
										for (int k = num5; k <= num6; k++)
										{
											num2 = 53;
											streamWriter.Write("<td>&nbsp;</td>");
											num2 = 54;
										}
										num2 = 55;
										streamWriter.WriteLine("</TR>");
									}
									num2 = 56;
								}
								num2 = 57;
							}
							num2 = 58;
							if (enumerator2 is IDisposable)
							{
								(enumerator2 as IDisposable).Dispose();
							}
							num2 = 59;
							streamWriter.WriteLine("</BODY></HTML>");
							num2 = 60;
							streamWriter.Close();
							num2 = 61;
							clsShared.cFile.RunFile(filename);
							num2 = 62;
							cDelay.Delay(2f);
							num2 = 63;
							clsShared.cSubs.NormalMouse();
						}
					}
				}
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num7 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_0606: Could not find block for branch target IL_05d2*/;
			}
			if (num7 != 0)
			{
				ProjectData.ClearProjectError();
			}
		}
	}
}
